package todomanager.CONTROLLER;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;

import todomanager.CONTROLLER.AddFrameController.AddMoreAction;
import todomanager.CONTROLLER.AddFrameController.AddTaskAction;
import todomanager.CONTROLLER.AddFrameController.CancelAction;
import todomanager.MODEL.TodoManagerModel;
import todomanager.View.EditFrame;
import todomanager.View.IconManager;
import todomanager.View.MainFrame;
import todomanager.data.TodoData;

/**
 * A controller for the edit frame
 * 
 * @author shirin
 * 
 */

public class EditFrameController {

	private TodoManagerModel todoModel;
	private MainFrame mainFrameView;

	private Action EditTaskAction;
	private Action cancelAction;

	private EditFrame editFrame;

	/**
	 * Standard constructor for the edit frame
	 * 
	 * @param model
	 *            the TodoManagerModel for database interaction
	 * @param view
	 *            the main frame for updating the table and so on.
	 * @throws UnsupportedEncodingException 
	 */
	public EditFrameController(TodoManagerModel model, MainFrame view) throws UnsupportedEncodingException {
		this.todoModel = model;
		this.mainFrameView = view;

		IconManager icm = new IconManager();

		this.EditTaskAction = new editTaskAction(
				view.getLabel("editTaskButton"), null);
		// this.EditTaskAction = new editTaskAction("editTaskButton",null);
		this.cancelAction = new CancelAction(view.getLabel("cancelButton"),
				null);

		// Add Action
		this.mainFrameView.getEditView().editButtonActions(this.EditTaskAction,
				this.cancelAction);
	}

	/**
	 * Action for editing a task to the database
	 * 
	 * @author shirin
	 * 
	 */
	public class editTaskAction extends AbstractAction {

		public editTaskAction(String text, ImageIcon icon) throws UnsupportedEncodingException {
			super(text, icon);
			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("editTaskButton_Desc"));

		}

		public void actionPerformed(ActionEvent e) {
			try {
				todoModel.editTask(mainFrameView.getEditView().getUserInput(),
						mainFrameView.getEditView().getInput());
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			mainFrameView.updateTableData();
			mainFrameView.getEditView().setVisible(false); 
			
		}
	}

	/**
	 * Action to cancel editing a task.
	 * 
	 * @author shirin
	 * 
	 */
	public class CancelAction extends AbstractAction {

		public CancelAction(String text, ImageIcon icon) throws UnsupportedEncodingException {
			super(text, icon);
			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("cancelButton_Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			mainFrameView.getEditView().setVisible(false);
		}
	}

}
